package org.example.agent;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;

import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;

public class AgentMethod {

    public static void premain(String args, Instrumentation inst){
        System.out.println("准备开始代理");
        System.out.println("代理参数："+args);
        AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule) -> {
            //对所有方法进行拦截
            return builder.method(ElementMatchers.any())
                    //指定拦截器
                    .intercept(MethodDelegation.to(RequestTimeInterceptor.class));
        };

        //拦截指定包下的方法
        new AgentBuilder.Default().type(ElementMatchers.nameStartsWith("org.example.user"))
                .transform(transformer).installOn(inst);

    }
}
